/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdcache.distributed.managment;

import com.sun.enterprise.ee.cms.core.DistributedStateCache;
import com.sun.enterprise.ee.cms.core.GMSConstants.shutdownType;
import com.sun.enterprise.ee.cms.core.GMSException;
import com.sun.enterprise.ee.cms.core.GMSFactory;
import com.sun.enterprise.ee.cms.core.GroupManagementService;
import com.sun.enterprise.ee.cms.impl.client.FailureNotificationActionFactoryImpl;
import com.sun.enterprise.ee.cms.impl.client.JoinNotificationActionFactoryImpl;
import com.sun.enterprise.ee.cms.impl.client.MessageActionFactoryImpl;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author chaitanya
 */
public class ShoalManager {

    private GroupManagementService gms;
    private String groupName;
    private DistributedStateCache ds;

    public ShoalManager(String name) throws GMSException
    {
        this.groupName=name;
        gms = (GroupManagementService) GMSFactory.startGMSModule(System.nanoTime() + "", name, GroupManagementService.MemberType.CORE, null);
        gms.join();
        gms.addActionFactory(new MessageActionFactoryImpl(new MessageCallback()), name);
        //gms.getGroupHandle().getAllCurrentMembers()
    }

    public void sendMessage(byte[] bytes) throws GMSException
    {
        gms.getGroupHandle().sendMessage(groupName, bytes);
    }

    /**
     * @param args the command line arguments
     */
//    public static void main(String[] args) {
//        //gms.getGroupHandle().
//        try {
//            gms.join();
//            MyCallBack mcb = new MyCallBack();
//            gms.addActionFactory(new MessageActionFactoryImpl(mcb), "RH");
//            gms.addActionFactory(new JoinNotificationActionFactoryImpl(mcb) {
//            });
//            gms.addActionFactory(new FailureNotificationActionFactoryImpl(mcb) {
//            });
//            try {
//                Thread.sleep(7000);
//                gms.getGroupHandle().sendMessage("RH",(System.currentTimeMillis()+"Testing message").getBytes());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        } catch (GMSException ex) {
//            Logger.getLogger(ShoalManager.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
}
